<template>
  <el-card style="max-width: 90vw; max-height: 70px">
    <el-form ref="queryFormRef" :model="queryParams" :inline="true">
      <el-form-item prop="keywords" label="关键字">
        <el-input
          v-model="queryParams.keywords"
          placeholder="角色名称"
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery"
          ><i-ep-search />搜索</el-button
        >
        <el-button @click="handleResetQuery"><i-ep-refresh />重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="max-width: 90vw">
    <template #header>
      <div class="card-header">
        <el-button type="success" @click="addList">新增</el-button>
        <el-button
          type="danger"
          :disabled="ids.length == 0 ? true : false"
          @click="handleDelete()"
          >删除</el-button
        >
      </div>
    </template>
    <!-- 表格 -->
    <el-table
      :data="PageList"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="角色名称" />
      <el-table-column prop="code" label="角色编码" width="150" />
      <el-table-column label="状态" width="130">
        <template #default="scope">
          <el-popover
            effect="light"
            trigger="hover"
            placement="top"
            width="auto"
          >
            <template #reference v-if="scope.row.status">
              <el-tag type="success">{{ "正常" }}</el-tag>
            </template>
            <template #reference v-else>
              <el-tag type="warning">{{ "异常" }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="130" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button link size="small" type="primary"> 分配权限 </el-button>
          <el-button
            link
            size="small"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="primary"
            link
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="currentPage4"
        v-model:page-size="pageSize4"
        :page-sizes="[2, 5, 10, 15]"
        :small="small"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
  <!-- 新增/编辑弹窗 -->
  <el-dialog
    v-model="dialogFormVisible"
    :title="flag ? '新增角色' : '修改角色'"
    width="500"
  >
    <template #footer>
      <div class="dialog-footer">
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="form"
          label-width="auto"
          class="demo-ruleForm"
          status-icon
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色编码" prop="code">
            <el-input v-model="form.code" placeholder="请输入角色编码" />
          </el-form-item>
          <el-form-item label="数据权限" prop="dataScope">
            <el-select v-model="form.dataScope" placeholder="请选择">
              <el-option :key="0" label="全部数据" :value="0" />
              <el-option :key="1" label="部门及子部门数据" :value="1" />
              <el-option :key="2" label="本部门数据" :value="2" />
              <el-option :key="3" label="本人数据" :value="3" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="resource">
            <el-radio-group v-model="form.status">
              <el-radio :value="1">正常</el-radio>
              <el-radio :value="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number
              v-model="form.sort"
              class="mx-4"
              :min="1"
              :max="10"
              controls-position="right"
            />
          </el-form-item>
        </el-form>
        <el-button v-if="flag" type="primary" @click="add"> 确定 </el-button>
        <el-button v-else type="primary" @click="update"> 确定 </el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
const input = ref("");
import { RolePageVO, RoleForm, RoleQuery } from "@/api/role/model";
import {
  getPage,
  addPage,
  getBack,
  updateList,
  delList,
} from "@/api/role/index";

const queryFormRef = ref(ElForm);

const currentPage4 = ref(1);
const pageSize4 = ref(10);
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
const PageList = ref([]);
const total = ref(0);
const oid = ref(0);
const ids = ref([]);
const flag = ref(false); //控制弹窗文字展示
const dialogFormVisible = ref(false);
// const formLabelWidth = "140px";
const form = ref({
  name: "",
  code: "",
  dataScope: "",
  status: 1,
  sort: 1,
});

const queryParams = reactive<RoleQuery>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  getPageList();
});

const getPageList = async () => {
  let res = await getPage(queryParams);
  PageList.value = Object.values(res)[0];
  total.value = res.total;
};
// 分页
const handleSizeChange = async (val: number) => {
  queryParams.pageSize = val;
  queryParams.pageNum = 1;
  let res = await getPage(queryParams);
  PageList.value = Object.values(res)[0];
};
const handleCurrentChange = async (val: number) => {
  queryParams.pageNum = val;
  let res = await getPage(queryParams);
  PageList.value = Object.values(res)[0];
};

// 显示添加弹窗
const addList = () => {
  flag.value = true;
  dialogFormVisible.value = true;
  reForm();
};
// 确定添加
const add = async () => {
  dialogFormVisible.value = false;
  console.log(form.value);
  await addPage(form.value);
  render();
};
// 显示修改弹窗
const handleEdit = async (index: number, row: any) => {
  let res = await getBack(row.id);
  form.value = res;
  // delete form.value.id;
  flag.value = false;
  dialogFormVisible.value = true;
  oid.value = row.id;
};
// 确定修改
const update = async () => {
  console.log(form.value, oid);
  await updateList(oid.value, form.value).then(() => {
    ElMessage.success("修改成功");
    dialogFormVisible.value = false;
    render();
  });
};
/** 行checkbox 选中事件 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
  console.log(ids.value);
}

// 点击删除
const handleDelete = async (roleId?: number) => {
  const roleIds = [roleId || ids.value].join(",");
  if (!roleIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    delList(roleIds).then(() => {
      ElMessage.success("删除成功");
      render();
    });
  });
};

// 搜索
const handleQuery = async () => {
  let res = await getPage(queryParams);
  PageList.value = res.list;
  total.value = res.total;
  render();
};
// 重置搜索
const handleResetQuery = () => {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
};

// 更新数据
const render = () => {
  getPageList();
};
// 重置表单
const reForm = () => {
  form.value = {
    name: "",
    code: "",
    dataScope: "",
    status: 1,
    sort: 1,
  };
};
</script>
<style scoped lang="less">
.el-card {
  margin-bottom: 20px;
}
</style>